#!user/bin/env python3
#!_*_conding:utf-8_*_
#!@uthor:阿健
#! @time:2019-12-20 下午 7:41
import requests,json,jsonpath,re,pymysql,time

def content():
    headers={
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
    }
    for i in range(1,107):
        #base_url="http://2.push2.eastmoney.com/api/qt/clist/get?cb=jQuery1124007597275279695137_1577345410047&pn={}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:116+t:3&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152&_=1577345410109".format(i)
        base_url="http://11.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112409935643657748849_1577412892901&pn={}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:116+t:3&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152&_=1577412892918".format(i)
        req=requests.get(url=base_url,headers=headers)
        req.encoding=req.apparent_encoding
        reqs=req.text
        # print(reqs)
        req=re.compile(r'(\[{.+?}\])',re.S)
        html = re.findall(req,reqs)
        # print(html[0])
        html=json.loads(html[0])
        # print(html)
        for i in html:
            item=[i["f12"],i["f14"],i["f2"],i["f4"],i["f3"],i["f18"],i["f15"],i["f16"],i["f17"],i["f5"],i["f6"]]
            print(item)
            mysql_insert(item)

def mysql_insert(item):
    db=pymysql.connect(host="192.168.0.172",user="root",password="123456",db="finance",charset="utf8")   #106.12.30.33  数据库名ykq 密码mbsb74194741
    cursor = db.cursor()
    try:
        time.sleep(0.5)
        sql = "insert into caifu1 values(0,'{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(item[0],item[1],item[2],item[3],item[4],item[5],item[6],item[7],item[8],item[9],item[10])
        cursor.execute(sql)
        db.commit()

    except:
        pass
    db.close()

if __name__ == '__main__':
    content()











